package com.dongqi.deviceData.position.service.impl;

import com.alibaba.fastjson.JSON;
import com.dongqi.constants.Constants;
import com.dongqi.deviceData.entity.*;
import com.dongqi.deviceData.mapper.*;
import com.dongqi.deviceData.position.dto.*;
import com.dongqi.deviceData.position.service.PositionService;
import com.dongqi.utils.JsonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class PositionServiceImpl implements PositionService {
    @Autowired
    private DeviceVehicleMapper deviceVehicleMapper;

    @Autowired
    private DeviceHydPressureMapper deviceHydPressureMapper;

    @Autowired
    private DeviceGuardrailBoardMapper deviceGuardrailBoardMapper;

    @Autowired
    private DeviceMeasureMapper deviceMeasureMapper;

    @Autowired
    private DevicePositionMapper devicePositionMapper;


    @Override
    public List<Object> queryAll() {
        //判断是否是轨迹，是轨迹就返回最近12小时内的轨迹集合，不是轨迹就返回最后一次的未知
        List<Object> list = new ArrayList<>();
        //车辆
        List<DeviceVehicleEntity> deviceVehicleEntities = deviceVehicleMapper.selectList(null);
        if (deviceVehicleEntities.size() > 0) {
            for (DeviceVehicleEntity entity : deviceVehicleEntities) {
                DeviceVehicleDTO deviceVehicleDTO = new DeviceVehicleDTO();
                deviceVehicleDTO.setCarModel(entity.getCarModel());
                deviceVehicleDTO.setCarNo(entity.getCarNo());
                deviceVehicleDTO.setDeviceNo(entity.getDeviceNo());
                deviceVehicleDTO.setDeviceType(Constants.DEVICE_VEHICLE);
                deviceVehicleDTO.setDeviceStatus(entity.getStatus());
                deviceVehicleDTO.setLongitude(entity.getLongitude());
                deviceVehicleDTO.setLatitude(entity.getLatitude());
                deviceVehicleDTO.setTimestp(entity.getTimestp());
                getPositionResult(deviceVehicleDTO, null, null, null, list, entity.getDeviceNo());
            }
        }
        //液压设备
        List<DeviceHydPressureEntity> deviceHydPressureEntities = deviceHydPressureMapper.selectList(null);
        if (deviceHydPressureEntities.size() > 0) {
            for (DeviceHydPressureEntity entity : deviceHydPressureEntities) {
                DeviceHydPressureDTO deviceHydPressureDTO = new DeviceHydPressureDTO();
                deviceHydPressureDTO.setDeviceNo(entity.getDeviceNo());
                deviceHydPressureDTO.setDeviceType(Constants.DEVICE_HYD_PRESSURE);
                deviceHydPressureDTO.setDeviceStatus(entity.getStatus());
                deviceHydPressureDTO.setHydModel(entity.getHydModel());
                deviceHydPressureDTO.setHydNo(entity.getHydNo());
                deviceHydPressureDTO.setLatitude(entity.getLatitude());
                deviceHydPressureDTO.setLongitude(entity.getLongitude());
                deviceHydPressureDTO.setTimestp(entity.getTimestp());
                getPositionResult(null, null, deviceHydPressureDTO, null, list, entity.getDeviceNo());
            }
        }
        //测量设备
        List<DeviceMeasureEntity> deviceMeasureEntities = deviceMeasureMapper.selectList(null);
        if (deviceMeasureEntities.size() > 0) {
            for (DeviceMeasureEntity entity : deviceMeasureEntities) {
                DeviceMeasureDTO deviceMeasureDTO = new DeviceMeasureDTO();
                deviceMeasureDTO.setDeviceNo(entity.getDeviceNo());
                deviceMeasureDTO.setDeviceType(Constants.DEVICE_MEASURE);
                deviceMeasureDTO.setDeviceStatus(entity.getStatus());
                deviceMeasureDTO.setMeasureModel(entity.getMeasureModel());
                deviceMeasureDTO.setMeasureNo(entity.getMeasureNo());
                deviceMeasureDTO.setLatitude(entity.getLatitude());
                deviceMeasureDTO.setLongitude(entity.getLongitude());
                deviceMeasureDTO.setTimestp(entity.getTimestp());
                getPositionResult(null, deviceMeasureDTO, null, null, list, entity.getDeviceNo());
            }
        }
        //护拦板设备
        List<DeviceGuardrailBoardEntity> deviceGuardrailBoardEntities = deviceGuardrailBoardMapper.selectList(null);
        if (deviceGuardrailBoardEntities.size() > 0) {
            for (DeviceGuardrailBoardEntity entity : deviceGuardrailBoardEntities) {
                DeviceGuardrailBoardDTO deviceGuardrailBoardDTO = new DeviceGuardrailBoardDTO();
                deviceGuardrailBoardDTO.setDeviceNo(entity.getDeviceNo());
                deviceGuardrailBoardDTO.setDeviceType(Constants.DEVICE_GUARDRAIL);
                deviceGuardrailBoardDTO.setDeviceStatus(entity.getStatus());
                deviceGuardrailBoardDTO.setEquipModel(entity.getEquipModel());
                deviceGuardrailBoardDTO.setBoardNo(entity.getBoardNo());
                deviceGuardrailBoardDTO.setLatitude(entity.getLatitude());
                deviceGuardrailBoardDTO.setLongitude(entity.getLongitude());
                deviceGuardrailBoardDTO.setTimestp(entity.getTimestp());
                getPositionResult(null, null, null,deviceGuardrailBoardDTO, list, entity.getDeviceNo());
            }
        }
        return list;
    }

    private List<Object> getPositionResult(DeviceVehicleDTO vihicleDTO,
                                           DeviceMeasureDTO measureDTO,
                                           DeviceHydPressureDTO pressureDTO,
                                           DeviceGuardrailBoardDTO guardrailBoardDTO,
                                           List<Object> list, String deviceNo) {
        //根据设备唯一标识查询位置表
        List<DevicePositionEntity> devicePositionEntities = devicePositionMapper.queryRecent12hByDeviceNo(deviceNo);
        if (devicePositionEntities.size() > 0 ) {
            if (null != vihicleDTO) {
                List<Object> dtoList = getPositonDTO(devicePositionEntities);
                HashMap<Object, Object> map = new HashMap<>();
                map.put("deviceInfo", vihicleDTO);
                if (dtoList.size()>0){
                    map.put("trajectoryInfo", dtoList);
                }
                list.add(map);
            }
            if (null != measureDTO) {
                List<Object> dtoList = getPositonDTO(devicePositionEntities);
                HashMap<Object, Object> map = new HashMap<>();
                map.put("deviceInfo", measureDTO);
                if (dtoList.size()>0){
                    map.put("trajectoryInfo", dtoList);
                }
                list.add(map);
            }
            if (null != pressureDTO) {
                List<Object> dtoList = getPositonDTO(devicePositionEntities);
                HashMap<Object, Object> map = new HashMap<>();
                map.put("deviceInfo", pressureDTO);
                if (dtoList.size()>0){
                    map.put("trajectoryInfo", dtoList);
                }
                list.add(map);

            }
            if (null != guardrailBoardDTO) {
                List<Object> dtoList = getPositonDTO(devicePositionEntities);
                HashMap<Object, Object> map = new HashMap<>();
                map.put("deviceInfo", guardrailBoardDTO);
                if (dtoList.size()>0){
                    map.put("trajectoryInfo", dtoList);
                }
                list.add(map);
            }

        } else {
            if (null != vihicleDTO) {
                HashMap<Object, Object> map = new HashMap<>();
                map.put("deviceInfo", vihicleDTO);
                list.add(map);

            }
            if (null != guardrailBoardDTO) {
                HashMap<Object, Object> map = new HashMap<>();
                map.put("deviceInfo", guardrailBoardDTO);
                list.add(map);

            }
            if (null != pressureDTO) {
                HashMap<Object, Object> map = new HashMap<>();
                map.put("deviceInfo", pressureDTO);
                list.add(map);

            }
            if (null != measureDTO) {
                HashMap<Object, Object> map = new HashMap<>();
                map.put("deviceInfo", measureDTO);
                list.add(map);
            }
        }
        return list;
    }

    public List<Object> getPositonDTO(List<DevicePositionEntity> entities) {
        ArrayList<Object> returnList = new ArrayList<>();
        if (entities.size() > 0 && entities.size() == 1) {
            PositionDTO dto = new PositionDTO();
            BeanUtils.copyProperties(entities.get(0), dto);
            returnList.add(dto);
        } else if (entities.size() > 1) {
            for (DevicePositionEntity entity : entities) {
                PositionDTO dto = new PositionDTO();
                BeanUtils.copyProperties(entity, dto);
                returnList.add(dto);
            }
        }
        return returnList;
    }

}
